keep = ls()

################################
#Make Iowa Legislator Vote Data#
################################

#load data
legislators = fread('./raw/iowa_legislator_data.csv') %>%
  .[Name != ""] %>%
  .[Chamber %in% "House"]

#clean
legislators[, birth_year := as.numeric(`YEAR OF BIRTH`)]
legislators[, birth_state := `STATE OF BIRTH`]
legislators[, union_veteran := `UNION ARMY (Y)` %in% "Y"]
legislators[, party := Party]
legislators[, previous_legislator := `FIRST TIME IN OFFICE (N)` %in% "N"]
legislators[, republican := (party %in% 'Republican')]

#Clean Votes
vote_vars = names(legislators)[str_detect(names(legislators), "Vote")]
legislators[, c(vote_vars) := lapply(.SD, function(x) ifelse(x %in% 'y', 1, ifelse(x %in% 'n', 0, NA))), .SDcols = vote_vars]
legislators[, some_vote := !(.SD %>% is.na %>% apply(1, all)), .SDcols = paste0("Vote_", 15:22)]

#create suffrage vote indicators
legislators[, full_suff := (Vote_15 == 0)  & (Vote_17 ==1)] 
legislators[, full_equal := (Vote_15 == 0)  & (Vote_17==1) & (Vote_18==1)] 


#Split into counties
legislators[, leg_id := 1:.N]
cty_leg = legislators[, list(county = str_split(COUNTY, ";\\s?") %>% unlist),
               by = list(leg_id, party, republican, previous_legislator, union_veteran, full_suff, full_equal)]


#Load and merge county data
ia_referenda_data = fread("./cleaned/ia_referenda_use.csv")

setkey(cty_leg, county)
setkey(ia_referenda_data, county_u)
ia_leg_use = ia_referenda_data[cty_leg] %>%
  .[, list(vet_pct = sum(vets_alt, na.rm = T) / sum(mil_age, na.rm = T),
           c_company_casualty_rate = weighted.mean(c_company_casualty_rate, vets_alt, na.rm = T),
           c_company_kia = weighted.mean(c_company_kia, vets_alt, na.rm = T),
           c_combat_days = weighted.mean(c_combat_days, vets_alt, na.rm = T),
           yes_57 = sum(suff57_yes_count, na.rm = T)/sum(gov57_r_count + gov57_d_count, na.rm = T),
           no_57 = sum(suff57_no_count, na.rm = T)/sum(gov57_r_count + gov57_d_count, na.rm = T),
           r_57 = sum(gov57_r_count, na.rm = T)/sum(gov57_r_count + gov57_d_count, na.rm = T),
           d_57 = sum(gov57_d_count, na.rm = T)/sum(gov57_r_count + gov57_d_count, na.rm = T),
           r_65 = sum(gov65_r_count, na.rm = T)/sum(gov65_r_count + gov65_d_count, na.rm = T),
           chg = sum(gov65_denom_count, na.rm = T)/ sum(gov57_denom_count, na.rm = T),
           n = sum(gov57_denom_count)), 
    by = list(leg_id, party, republican, previous_legislator, union_veteran, full_suff, full_equal)]

fwrite(ia_leg_use, "./cleaned/ia_legislators_use.csv")

#Cleanup
rm(list = setdiff(ls(), c(keep, 'keep')))
gc()